package ec.espol.edu.isi.grupo2.academicoicefenix;
import java.util.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;

public abstract class WsClient {
	private static HttpTransportSE httpTransport = new HttpTransportSE("https://ws.espol.edu.ec/saac/wsandroid.asmx");
	private static String namespace = "http://tempuri.org/";
	
	public static Usuario wsInfoUsuario(String usuario) {
		String action = "wsInfoUsuario";
		
		SoapObject request = new SoapObject(namespace, action);
		request.addProperty("usuario", usuario);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		SoapObject response;
		try {
			httpTransport.call(namespace+action, envelope);
			
			response = (SoapObject) envelope.getResponse();
			response = (SoapObject) response.getProperty("diffgram");
			
			if (response.getPropertyCount() == 0)
				return null;
			
			response = (SoapObject) response.getProperty("NewDataSet");
			response = (SoapObject) response.getProperty("INFORMACION_x0020_USUARIO");
			
			return new Usuario(usuario, response.getProperty("NOMBRES").toString().trim()+" "+response.getProperty("APELLIDOS").toString().trim(),
							   response.getProperty("TIPO_IDENTIF").toString().trim(),
							   response.getProperty("NRO_IDENTIF").toString().trim(),
							   response.getProperty("TIPO").toString().trim());
		} catch (Exception e) {
			return null;
		}
	}
	
	public static ArrayList<Estudiante> wsEstudiantesRegistrados(String codigoMateria,int paralelo) {
		String action = "wsEstudiantesRegistrados";
		
        SoapObject request = new SoapObject(namespace, action);
        request.addProperty("codigoMateria", codigoMateria);
        request.addProperty("paralelo", paralelo);
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        SoapObject response;
        try {
            httpTransport.call(namespace+action, envelope);

            response = (SoapObject) envelope.getResponse();
            response = (SoapObject) response.getProperty("diffgram");
			
			if (response.getPropertyCount() == 0)
				return null;
			
            response = (SoapObject) response.getProperty("NewDataSet");
            
			ArrayList<Estudiante> listaEstudiante = new ArrayList <Estudiante> ();
			String nombre, matricula;
            Estudiante estudiante;
			SoapObject temp1;
			
			for(int i=0; i<response.getPropertyCount(); i++) {
                temp1 = (SoapObject) response.getProperty(i);
				nombre = temp1.getProperty("_x0031_").toString().trim();
				matricula = temp1.getProperty("COD_ESTUDIANTE").toString().trim();
				estudiante = new Estudiante(nombre, matricula);
                listaEstudiante.add(estudiante);
            }
			
            return listaEstudiante;
        } catch (Exception e) {
            return null;
        }
    }
	
	public static String wsInfoEstudiante(String codigo) {
		String action = "wsInfoEstudiante";

		SoapObject request = new SoapObject(namespace, action);
		request.addProperty("codigoEstudiante", codigo);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		SoapObject response;
		try {
			httpTransport.call(namespace+action, envelope);

			response = (SoapObject) envelope.getResponse();
			response = (SoapObject) response.getProperty("diffgram");

			if (response.getPropertyCount() == 0)
				return "Error: Count 0";

			response = (SoapObject) response.getProperty("NewDataSet");
			
			return response+"";
		} catch (Exception e) {
			return "Excepcion: "+e;
		}
	}
	
	public static String wsInfoMateria(String codigo, int paralelo) {
		String action = "wsInfomateria";

		SoapObject request = new SoapObject(namespace, action);
		request.addProperty("codigoMateria", codigo);
		request.addProperty("paralelo", paralelo);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		SoapObject response;
		try {
			httpTransport.call(namespace+action, envelope);

			response = (SoapObject) envelope.getResponse();
			response = (SoapObject) response.getProperty("diffgram");

			if (response.getPropertyCount() == 0)
				return "Error: Count 0";

			response = (SoapObject) response.getProperty("NewDataSet");

			return response+"";
		} catch (Exception e) {
			return "Excepcion: "+e;
		}
	}
	
	public static String wsMateriasADictar(int idProfesor) {
		String action = "wsMateriasADictar";

		SoapObject request = new SoapObject(namespace, action);
		request.addProperty("idProfesor", idProfesor);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		SoapObject response;
		try {
			httpTransport.call(namespace+action, envelope);

			response = (SoapObject) envelope.getResponse();
			response = (SoapObject) response.getProperty("diffgram");

			if (response.getPropertyCount() == 0)
				return "Error: Count 0";

			response = (SoapObject) response.getProperty("NewDataSet");

			return response+"";
		} catch (Exception e) {
			return "Excepcion: "+e;
		}
	}
}
